namespace Resharper.Abstractions.CodeDom.Expressions.LiteralExpressions
{
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Psi.Tree;

  /// <summary>Defines the <see cref="LiteralExpression"/> class.</summary>
  public class LiteralExpression : Expression
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="LiteralExpression"/> class.</summary>
    /// <param name="literalExpression">The expression.</param>
    internal LiteralExpression([NotNull] ILiteralExpression literalExpression) : base(literalExpression)
    {
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets the constant value.
    /// </summary>
    [CanBeNull]
    public string Value
    {
      get
      {
        if (!this.IsConstantValue)
        {
          return null;
        }

        var constantValue = this.InternalLiteralExpression.ConstantValue;
        if (!constantValue.IsString())
        {
          return null;
        }

        return constantValue.Value as string;
      }
    }

    /// <summary>The literal expression field.</summary>
    [NotNull]
    internal ILiteralExpression InternalLiteralExpression
    {
      get
      {
        return (ILiteralExpression)this.InternalExpression;
      }
    }

    #endregion
  }
}